//
// Created by chaseaishang on 23-5-18.
//

#pragma once

#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <event2/bufferevent.h>
#include <event2/event.h>
#include <arpa/inet.h>
#include "Gamedata.h"



class Portocol
{
public:
    ~Portocol();
    void init();

    bool sendToServer(char*,int len,game_state state);
    game_state recvFromServer(char*,int &len,GameData::In_gameData *gamedata= nullptr);
    game_state recvFromServerNoBlocking(char*,int &len,GameData::In_gameData *gamedata= nullptr);
    void unpackData(char*,int&);
    GameData::In_gameData GetgameData();
    bool ready=false;
    game_state m_state;
public:

    int m_fd;

    struct event* read_ev;
private:
    pthread_mutex_t m_mutex;
    pthread_mutex_t m_Write_mutex;
    char m_message[50];
    // need lock
    GameData::In_gameData gamedata;
    bool could_read=false;
    bool could_write=false;
private:
    static void on_read(int sock, short event, void* arg);
    static void* init_read_event(void* arg);
    void init_read_event_thread();
};



